package com.netted.operationassistant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.netted.operationassistant.R;

import android_serialport_api.sample.ConsoleActivity;
import android_serialport_api.sample.LoopbackActivity;

public class SerialPortActivity extends BaseActivity {


    /** Called when the com.netted.studentbook.activity.activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final Button buttonSetup = (Button)findViewById(R.id.ButtonSetup);
        buttonSetup.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
//                startActivity(new Intent(SerialPortActivity.this, SerialPortPreferences.class));
                getPortCount(  0);

                try {
                    Thread.sleep(1000);
                    if (!"".equals(serialValue)){
                        boolean b = verify(serialValue);
                        if(b){
                            String[] arr = serialValue.split(" ");

                            buttonSetup.setText("0号板仓口数量:"+Integer.parseInt(arr[arr.length - 3], 16));
                        }else{
                            showDialog("提示","校验错误",null);
                        }
                    }else{
                        showDialog("提示","无应答,该板不存在!",null);
                    }
                    serialValue = "";
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });

        final Button buttonConsole = (Button)findViewById(R.id.ButtonConsole);
        buttonConsole.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(SerialPortActivity.this, ConsoleActivity.class));
            }
        });

        final Button buttonLoopback = (Button)findViewById(R.id.ButtonLoopback);
        buttonLoopback.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(SerialPortActivity.this, LoopbackActivity.class));
            }
        });

        final Button button01010101 = (Button)findViewById(R.id.Button01010101);
        button01010101.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
//                startActivity(new Intent(SerialPortActivity.this, Sending01010101Activity.class));
//                getBuffer();
            }
        });

        final Button buttonAbout = (Button)findViewById(R.id.ButtonAbout);
        buttonAbout.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
//                AlertDialog.Builder builder = new AlertDialog.Builder(SerialPortActivity.this);
//                builder.setTitle("About");
//                builder.setMessage(R.string.about_msg);
//                builder.show();

//                startActivity(new Intent(SerialPortActivity.this, PhysicaloidTestActivity.class));

                openPort("00","01");
                try {
                    Thread.sleep(1000);
                    if (!"".equals(serialValue)){
                        boolean b = verify(serialValue);
                        if(b){
                            buttonAbout.setText("打开0号板0号仓 收到指令:"+serialValue);
                        }else{
                            showDialog("提示","校验错误",null);
                        }
                    }else{
                        showDialog("提示","无应答",null);
                    }
                    serialValue = "";
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        final Button buttonQuit = (Button)findViewById(R.id.ButtonQuit);
        buttonQuit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                SerialPortActivity.this.finish();
//                test();
            }
        });
    }


    /*public void test(){
        // Find all available drivers from attached devices.
        UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
        List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager);
        if (availableDrivers.isEmpty()) {
            return;
        }

// Open a connection to the first available driver.
        UsbSerialDriver driver = availableDrivers.get(0);
        UsbDeviceConnection connection = manager.openDevice(driver.getDevice());
        if (connection == null) {
            // You probably need to call UsbManager.requestPermission(driver.getDevice(), ..)
            return;
        }

// Read some data! Most have just one port (port 0).
        UsbSerialPort port = driver.getPorts().get(0);
        try {
            port.open(connection);
            port.setParameters(19200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);

            byte buffer[] = new byte[16];
            int numBytesRead = port.read(buffer, 1000);
            LogUtils.e("Read #############################" + numBytesRead + " bytes.");

        } catch (IOException e) {
            // Deal with error.
        } finally {
            try {
                port.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }*/
/*
    @Override
    public void doReadThread(String res) {
        super.doReadThread(res);
        showDialog("1",res,null);
    }*/
}
